package com.share.test.example.selenium.groovy

import com.share.common.utils.DelayUtil
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement

public Object parse(WebDriver driver) {
    //打开创建促销管理页面---需要加载页面
    String url = "https://sellercentral.amazon.com/promotions/manage#&pType=STATUS&tab=manage-your-promotions&psType=TRACKING_ID";
    driver.get(url);
    DelayUtil.sleepRangSecond(2, 5);

    //1.跳转到指定的页面
    By pageBarXpath = By.xpath("//*[@id=\"managepromotionstable-pagination\"]/ul/li");
    List<WebElement> ele_page_bar = driver.findElements(pageBarXpath);
    int startPage = 3; //取值为1-9,默认取值为3
    int dealSize = 2; //默认取值为1
    int num = 0; //计数器
    if (ele_page_bar.size() >= 3 + startPage) {//上一页,~,下一页,1,2,3,
        if (startPage != 1) {//第一页不需要点击
            ele_page_bar.get(startPage).click();
            DelayUtil.sleepRangSecond(2, 5);
        }

        do {
            //计数器
            num++;

            //分析数据
            List<WebElement> ele_trs = driver.findElements(By.xpath("//*[@id=\"managepromotionstable\"]/div[5]/div/table/tbody/tr"));
            int size = ele_trs.size();
            for (int i = 1; i < size; i++) {
                WebElement ele_tr = ele_trs.get(i);

                String rowId = ele_tr.getAttribute("id");
                WebElement ele_trackingIdField = driver.findElement(By.id(rowId + "-trackingIdField"));

                WebElement ele_startDateField = driver.findElement(By.id(rowId + "-startDateField"));
                WebElement ele_endDateField = driver.findElement(By.id(rowId + "-endDateField"));
                WebElement ele_ordersField = driver.findElement(By.id(rowId + "-ordersField"));

                String msg = ele_trackingIdField.getText() + "_" + ele_startDateField.getText() + "_" + ele_endDateField.getText() + "_" + ele_ordersField.getText();
                System.out.println(msg);
            }

            //提前退出
            if (num > dealSize) {
                break;
            }

            //翻页操作
            ele_page_bar = driver.findElements(pageBarXpath);
            WebElement ele_next_page = ele_page_bar.get(ele_page_bar.size() - 1);
            ele_next_page.click();
            DelayUtil.sleepRangSecond(2, 5);
        } while (num < dealSize);
    }
    return 0;
}
